/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rdr.geo.map2d.tiles;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 *
 * @author ruanor
 */
public class TileXmlReader {

    private String imageFileName;
    private long latitude;
    private long longitude;

    public String getImageFileName() {
        return imageFileName;
    }
    
    public long getLatitude(){
        return latitude;
    }
    
    public long getLongitude(){
        return longitude;
    }

    public void read(String xmlFileName) {

        try {
            File xmlFile = new File(xmlFileName);
            if (!xmlFile.exists()) {
                return;
            }

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File(xmlFileName));
            doc.getDocumentElement().normalize();

            Node firstNode = doc.getElementsByTagName("Tile").item(0);
            Element firstTileElement = (Element) firstNode;
            imageFileName = firstTileElement.getElementsByTagName("File").item(0).getChildNodes().item(0).getNodeValue();
            String slat = firstTileElement.getElementsByTagName("Lat").item(0).getChildNodes().item(0).getNodeValue();
            String slon = firstTileElement.getElementsByTagName("Lon").item(0).getChildNodes().item(0).getNodeValue();

            this.latitude = Long.parseLong(slat);
            this.longitude = Long.parseLong(slon);

            imageFileName = xmlFile.getParent() + File.separator + imageFileName;
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
